<?php
/**
 * 图片处理类
 * @package core
 * @subpackage class
 * @author
 * @version $Id$
 */
class Image extends Application {

	// ImageMagick路径
	public $_image_magick_path = '';
	public $_convert_command = '';
	public $_method = 'ImageMagick'; // "ImageMagick" Or "GD"
	public $_os = ''; //Unix Or Windows
	// 默认缩略图大小
	public $_thumb_wh = array(90,90);

	// 默认水印图片
	public $_watermarkfile = '';

	// 水印位置，0随机 1顶部居左 2顶部居右 3底部居左 4 底部居右
	public $_watermarkpos = 4;
	/**
	 * 构造函数
	 *
	 */
	public function __construct(){
		if ( substr(PHP_OS,0,3) == 'WIN' ){
			$this->_os = 'W';
		}
		if ( defined("IMAGE_MAGICK_PATH") ){
			$this->_image_magick_path = IMAGE_MAGICK_PATH;
			if ( substr($this->_image_magick_path,-1,1)!='/' ){
				$this->_image_magick_path .= '/';
			}
		}
		else {
			exit('未配置ImageMagick路径');
		}
		if ( $this->_os=='W' ){
			$this->_convert_command = $this->_image_magick_path.'convert.exe';
		}
		else {
			$this->_convert_command = $this->_image_magick_path.'convert';
		}
//		if ( is_executable($this->_convert_command) ){
//			print_r($this->_convert_command);
//			exit('ImageMagick不可用');
//		}
	}

	/**
	 * 生成缩略图
	 *
	 * @param string $srcfile
	 * @param string $destfile
	 * @param array $to_wh
	 */
	public function makeThumb($srcfile,$destfile='',$to_wh=array(90,90)) {
		if ( $this->_method=="ImageMagick" ){
			// 采用ImageMagick生成缩略图
			return $this->makeThumbByImagemagick($srcfile,$destfile,$to_wh);
		}
		elseif ( $this->_method=="GD" ) {
			// 采用GD生成缩略图
			return $this->makeThumbByGd($srcfile,$destfile,$to_wh);
		}
		else {
			exit('中配置的_method错误');
			return '';
		}
	}

	/**
	 * 使用ImageMagic生成缩略图
	 *
	 * @param unknown_type $srcfile
	 * @param unknown_type $destfile
	 * @param unknown_type $to_wh
	 * @return unknown
	 */
	public function makeThumbByImagemagick($srcfile,$destfile='',$to_wh=array(90,90)){
		//判断文件是否存在
		if ( !file_exists($srcfile) ) {
			throw_exception('源图片不存在');
			return '';
		}
		$src_file_info = @getimagesize($srcfile);
		if ( empty($src_file_info) ){
			throw_exception('获取源图片信息失败');
			return '';
		}
		if ( $destfile=="" ){
			$destfile = $srcfile;
		}
		if ( $this->_os=="W" ){
			$cmd = '"'.$this->_convert_command.'"';
		}
		else {
			$cmd = $this->_convert_command;
		}
		if ( $src_file_info[2]==1 ){
			//GIF 图片// add > by Eric;windows don't run
			if ( $this->_os=='W' ){
				$cmd .= ' -resize '.$to_wh[0].'x'.$to_wh[1].' '.$srcfile.' '.$destfile;
			}
			else {
				$cmd .= ' -resize "'.$to_wh[0].'x'.$to_wh[1].'>" '.$srcfile.' '.$destfile;
			}
		}
		else {
			if ( $this->_os=='W' ){
				$cmd .= ' -thumbnail '.$to_wh[0].'x'.$to_wh[1].' '.$srcfile.' '.$destfile;
			}
			else {
				$cmd .= ' -thumbnail "'.$to_wh[0].'x'.$to_wh[1].'>" '.$srcfile.' '.$destfile;
			}
		}
		$res = @exec ($cmd);
		if ( file_exists($destfile) ){
			return $destfile;
		}
		else {
			return '';
		}
	}

	/**
	 * 使用GD生成缩略图
	 *
	 * @param string	$srcfile	源文件
	 * @param string	$destfile	生成目标位置
	 * @param array		$to_wh		要生成的缩略图宽和高
	 * @return string
	 */
	public function makeThumbByGd($srcfile,$destfile='',$to_wh=array(90,90)){
		//判断文件是否存在
		if ( !file_exists($srcfile) ) {
			exit('源图片不存在');
			return '';
		}
		//缩略图大小
		$tow = intval($to_wh[0]);
		$toh = intval($to_wh[1]);
		//设置目标路径
		if ( !$destfile ){
			$destfile = $srcfile.'.thumb_'.$tow.'_'.$toh.'.jpg';
		}
		if($tow < 10){
			$tow = $this->_thumb_wh[0];
		}
		if($toh < 10){
			$tow = $this->_thumb_wh[1];
		}
		//获取图片信息
		$im = '';
		if( $data = getimagesize($srcfile) ) {
			if($data[2] == 1) {
				if(function_exists("imagecreatefromgif")) {
					$im = imagecreatefromgif($srcfile);
				}
			}
			elseif($data[2] == 2) {
				if(function_exists("imagecreatefromjpeg")) {
					$im = imagecreatefromjpeg($srcfile);
				}
			}
			elseif($data[2] == 3) {
				if(function_exists("imagecreatefrompng")) {
					$im = imagecreatefrompng($srcfile);
				}
			}
			else {
				$l = array('imagecreatefromgif','imagecreatefromjpeg','imagecreatefrompng','imagecreatefromwbmp','imagecreatefromxpm','imagecreatefromxbm','imagecreatefromwbmp','imagecreatefromgd2');
				foreach ($l as $v){
					if ( function_exists($v) ){
						$im = $v($srcfile);
						if ( $im ){
							break;
						}
					}
				}
			}
		}
		if( !$im ){
			exit('不能获取图片信息');
			return '';
		}
		//获取图片实际尺寸
		$srcw = imagesx($im);
		$srch = imagesy($im);
		$towh = $tow/$toh;
		$srcwh = $srcw/$srch;
		if($towh <= $srcwh){
			$ftow = $tow;
			$ftoh = $ftow*($srch/$srcw);
		}
		else {
			$ftoh = $toh;
			$ftow = $ftoh*($srcw/$srch);
		}
		if( $srcw>$tow || $srch>$toh ) {
			if( function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled")
			&& @$ni = imagecreatetruecolor($ftow, $ftoh) ) {
				//如果可以使用真彩色
				imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
			}
			elseif( function_exists("imagecreate") && function_exists("imagecopyresized")
			&& @$ni = imagecreate($ftow, $ftoh) ) {
				//使用基本色
				imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
			}
			else {
				return '';
			}
			if( function_exists('imagejpeg') ) {
				imagejpeg($ni, $destfile);
			}
			elseif(function_exists('imagepng')) {
				imagepng($ni, $destfile);
			}
			imagedestroy($ni);
		}
		else {
			if( function_exists('imagejpeg') ) {
				imagejpeg($im, $destfile);
			}
			elseif(function_exists('imagepng')) {
				imagepng($im, $destfile);
			}
		}
		imagedestroy($im);
		if( !file_exists($destfile) ) {
			return '';
		}
		else {
			return $destfile;
		}
	}

	/**
	 * 为图片增加水印
	 *
	 * @param string	$srcfile		源文件路径
	 * @param string	$watermarkfile	水印图片
	 * @param int		$watermarkpos	水印位置 0随机 1顶部居左 2顶部居右 3底部居左 4 底部居右
	 */
	public function makeWaterMark($srcfile,$watermarkfile='',$watermarkpos=-1) {
		if ( $watermarkfile=='' ){
			$watermarkfile = $this->_watermarkfile;
		}
		if ( !$watermarkfile ){
			exit('未提供水印图片');
			return '';
		}
		//水印图片
		if(!file_exists($watermarkfile) || !$water_info = getimagesize($watermarkfile)) {
			exit('水印图片不存在，或者不能获取图片信息');
			return '';
		}
		$water_w = $water_info[0];
		$water_h = $water_info[1];
		$water_im = '';
		switch($water_info[2]) {
			case 1:
				@$water_im = imagecreatefromgif($watermarkfile);
				break;
			case 2:
				@$water_im = imagecreatefromjpeg($watermarkfile);
				break;
			case 3:
				@$water_im = imagecreatefrompng($watermarkfile);
				break;
			default:
				break;
		}
		if(empty($water_im)) {
			exit('未知的水印图片格式');
			return '';
		}
		//原图
		if(!file_exists($srcfile) || !$src_info = getimagesize($srcfile)) {
			exit('源图片不存在，或者不能获取图片信息');
			return '';
		}
		$src_w = $src_info[0];
		$src_h = $src_info[1];
		$src_im = '';
		switch($src_info[2]) {
			case 1:
				//判断是否为动画
				$fp = fopen($srcfile, 'rb');
				$filecontent = fread($fp, filesize($srcfile));
				fclose($fp);
				if(strpos($filecontent, 'NETSCAPE2.0') === FALSE) {
					//动画图不加水印
					@$src_im = imagecreatefromgif($srcfile);
				}
				break;
			case 2:
				@$src_im = imagecreatefromjpeg($srcfile);
				break;
			case 3:
				@$src_im = imagecreatefrompng($srcfile);
				break;
			default:
				break;
		}
		if( empty($src_im) ) {
			exit('未知的源图片格式');
			return '';
		}
		//如果水印大小比原图小超过150，则不加水印
		if(($src_w < $water_w + 150) || ($src_h < $water_h + 150)) {
			return '';
		}
		if ( $watermarkpos<0||$watermarkpos>4 ){
			$watermarkpos = $this->_watermarkpos;
		}
		//位置
		switch( $watermarkpos ) {
			case 1://顶端居左
				$posx = 0;
				$posy = 0;
			break;
			case 2://顶端居右
				$posx = $src_w - $water_w;
				$posy = 0;
			break;
			case 3://底端居左
				$posx = 0;
				$posy = $src_h - $water_h;
			break;
			case 4://底端居右
				$posx = $src_w - $water_w;
				$posy = $src_h - $water_h;
			break;
			default://随机
				$posx = mt_rand(0, ($src_w - $water_w));
				$posy = mt_rand(0, ($src_h - $water_h));
			break;
		}
		//设定图像的混色模式
		@imagealphablending($src_im, true);
		//拷贝水印到目标文件
		@imagecopy($src_im, $water_im, $posx, $posy, 0, 0, $water_w, $water_h);
		switch($src_info[2]) {
			case 1:
				@imagegif($src_im, $srcfile);
				break;
			case 2:
				@imagejpeg($src_im, $srcfile);
				break;
			case 3:
				@imagepng($src_im, $srcfile);
				break;
			default:
				return '';
		}
		@imagedestroy($water_im);
		@imagedestroy($src_im);
	}
}
//类定义结束
?>